package com.summer.ioc.bean.definition.annotated;

import com.summer.ioc.bean.annotation.Autowired;
import com.summer.ioc.bean.definition.AbstractBeanDefinition;
import com.summer.ioc.reflect.Parameter;
import com.summer.ioc.util.AnnotationUtil;
import com.summer.ioc.util.MethodUtil;
import lombok.Getter;
import lombok.Setter;
import org.springframework.core.DefaultParameterNameDiscoverer;

import java.lang.reflect.Method;
import java.util.HashSet;
import java.util.Set;

/**
 * @author xiaoye
 * @create 2021-05-12 22:43
 *
 * 解析@Bean标注的方法生成的BeanDefinition
 */
@Setter
@Getter
public abstract class AbstractBeanAnnotatedBeanDefinition extends AbstractBeanDefinition {

    private Method method;

    private Object configurationClassObject;

    public AbstractBeanAnnotatedBeanDefinition(String name, Class beanClass) {
        super(name, beanClass);
    }

    @Override
    public Set<String> getDependencyBeanNames() {
        if (dependencyBeanNames != null)
            return dependencyBeanNames;
        dependencyBeanNames = new HashSet<>();
        DefaultParameterNameDiscoverer discover = new DefaultParameterNameDiscoverer();
        var parameters = MethodUtil.getParameters(method);
        if (AnnotationUtil.existAnnotation(method, Autowired.class))
        {
            for (Parameter parameter : parameters) {
                dependencyBeanNames.add(parameter.getName());
            }
        }
        else
        {
            for (Parameter parameter : parameters) {
                if (AnnotationUtil.existAnnotation(parameter.getParameter(),Autowired.class))
                    dependencyBeanNames.add(parameter.getName());
            }
        }
        return dependencyBeanNames;
    }
}
